package com.app.micaihu.view.army.header

import android.annotation.SuppressLint
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.view.View
import com.app.base.BaseHeaderView
import com.app.micaihu.R
import com.app.micaihu.configure.Config
import com.app.micaihu.manager.UserInforManager
import com.app.micaihu.view.army.activity.ArmyCreateActivity
import com.app.micaihu.view.army.activity.ArmyDetailActivity
import com.app.micaihu.view.bean.ArmyList
import com.app.micaihu.view.utils.loadImage
import com.app.utils.biz.Biz
import com.blankj.utilcode.util.ActivityUtils
import com.blankj.utilcode.util.ColorUtils
import com.jeremyliao.liveeventbus.LiveEventBus
import kotlinx.android.synthetic.main.army_join_no_header.view.*
import kotlinx.android.synthetic.main.view_legion_item.view.*

/**
 * 未加入军团 头部
 */
class NoJoinedArmyHeader(context: Context) : BaseHeaderView(context = context) {

    override fun getLayoutId() = R.layout.army_join_no_header

    private var myArmyGroup: ArmyList.MyArmyGroup? = null
    override fun setData() {
    }

    override fun addListeners() {
        llMyArmy.setOnClickListener {
            if (Biz.instance.uid().isEmpty()) {
                UserInforManager.getInstance().toLoginPage(ActivityUtils.getTopActivity())
                return@setOnClickListener
            }
            myArmyGroup?.let {
                val intent = Intent(ActivityUtils.getTopActivity(), ArmyDetailActivity::class.java)
                val bundle = Bundle()
                bundle.putString(Config.Extra.id, it.gid)
                bundle.putString(Config.Extra.toUid, Biz.instance.uid())
                intent.putExtras(bundle)
                ActivityUtils.startActivity(intent)
            }
        }

        llCreateLegion.setOnClickListener {
            if (Biz.instance.uid().isEmpty()) {
                UserInforManager.getInstance().toLoginPage(ActivityUtils.getTopActivity())
            } else {
                val intent = Intent(ActivityUtils.getTopActivity(), ArmyCreateActivity::class.java)
                ActivityUtils.startActivity(intent)
            }

        }
    }

    @SuppressLint("SetTextI18n")
    fun setMyArmyData(myArmyGroup: ArmyList.MyArmyGroup?) {
        this.myArmyGroup = myArmyGroup
        if (myArmyGroup == null) {
            llMyArmy.visibility = View.GONE
            llCreateLegion.visibility = View.VISIBLE
        } else {
            llMyArmy.visibility = View.VISIBLE
            llCreateLegion.visibility = View.GONE

            ivArmyIcon.loadImage(myArmyGroup.icon)
            tvArmyTitle.text = myArmyGroup.title
            tvDescription.text = myArmyGroup.description
            // 0待审核 1已通过 2拒绝 3退出或删除
            tvMember.setTextColor(ColorUtils.string2Int("#E51919"))
            when (myArmyGroup.status) {
                "0" -> {
                    tvMember.text = "待审核"
                    tvRefuse.visibility = View.GONE
                }
                "1" -> {
                    tvMember.text = "已通过"
                    tvRefuse.visibility = View.GONE
                }
                "2" -> {
                    tvMember.text = "已拒绝"
                    tvRefuse.visibility = View.GONE
                }
                "4" -> {
                    tvMember.text = "已拒绝"
                    tvRefuse.visibility = View.VISIBLE
                }
            }
            tvGid.text = "番号 ${myArmyGroup.gid}"
            tvWarZone.text = myArmyGroup.warZone
            tvLevel.text = myArmyGroup.level
            if (myArmyGroup.status == "1") {// 管理员同意后刷新状态
                LiveEventBus.get(Config.EventBus.joinArmySuccess).post(true)
            }
        }
    }

}